package com.ccp.dev.qbdms.tag;

import com.ccp.dev.core.util.AppUtil;
import com.ccp.dev.extend.service.ExtendSysOrgService;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.system.model.Dictionary;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.util.Arrays;
import java.util.List;

/**
 * @author 创新中心-王明亮
 * @description 数据来源单位标签
 */
public class SourceDeptTag extends SimpleTagSupport {

    /**
     * 下拉列表控件的名称
     */
    private String css;
    private String showIn;

    public String getCss() {
        return css;
    }

    public void setCss(String css) {
        this.css = css;
    }

    public String getShowIn() {
        return showIn;
    }

    public void setShowIn(String showIn) {
        this.showIn = showIn;
    }

    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();
        try {
            int numberLevel = BaseUtil.getCurrentSystemNumberLevel();
            String name = "";
            if (numberLevel == 0) {
                name = "Q_filldeptHospitalId_S";
            } else if (numberLevel == 1) {
                name = "Q_filldeptid_S";
            } else if (numberLevel == 2) {
                name = "Q_filldeptid_S";
            }
            StringBuffer stringBuffer = new StringBuffer();
            if (null == showIn) {
                showIn = "0,1";
            }
            List<String> showInList = Arrays.asList(showIn.split(","));
            if (showInList.contains(String.valueOf(numberLevel))) {
                stringBuffer.append("<div class=\"qbdms-listForm\">");
                stringBuffer.append("<label class=\"layui-form-label\">数据来源单位</label>");
                stringBuffer.append("<div class=\"layui-input-block\">");
                stringBuffer.append("<select ");
                if (css != null) {
                    stringBuffer.append("class=\"").append(css).append("\" ");
                }
                stringBuffer.append("name=\"").append(name).append("\" ");
                stringBuffer.append("placeholder=\"数据来源单位\"");
                stringBuffer.append("lay-search=\"\"");
                stringBuffer.append(">");
                stringBuffer.append("<option></option>");
                ExtendSysOrgService extendSysOrgService = AppUtil.getBean(ExtendSysOrgService.class);
                List<Dictionary> originOrgToDicList = extendSysOrgService.getSubordinateUnits();
                originOrgToDicList.forEach(dictionary -> {
                    stringBuffer.append("<option value=\"").append(dictionary.getItemValue()).append("\">").append(dictionary.getItemName()).append("</option>");
                });
                stringBuffer.append("</select>");
                stringBuffer.append("</div>");
                stringBuffer.append("</div>");
            }
            out.print(stringBuffer);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
